'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const stormpath = require('express-stormpath');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(stormpath.init(app, {
    expand: {
        customData: true,
    },
    web: {
        produces: ['application/json']
    },
    apiKey: {
        id: '5ZXY9YV0QRG5FCAQUIXLU455D',
        secret: 'M+imXOJKK2FbmbZMHLxp57L75MN9LoJnq88ar2AdQR0'
    },
    application: {
        href: 'https://api.stormpath.com/v1/applications/Ca0xZ04QxG6FRuaNLcyy5'
    }
}));
app.get('/notes', stormpath.apiAuthenticationRequired, function(req, res) {
    res.json({
        notes: req.user.customData.notes || "This is your notebook. Edit this to start saving your notes!"
    })
});
app.post('/notes', stormpath.apiAuthenticationRequired, function(req, res) {
    if (!req.body.notes || typeof req.body.notes != "string") {
        res.status(400).send("400 Bad Request")
    }
    if(!req.user.customData.notes) req.user.customData.notes = [];
    req.user.customData.notes.push(req.body.notes);
    req.user.customData.save()
    res.status(200).end()
});
app.on('stormpath.ready', function() {
    app.listen(3000, function(err) {
        if (!err) {
            console.log('server start at port 3000');
        } else {
            console.log(err);
        }
    })
})